import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/channel_model.dart';
import 'package:flutter_app_unionad/model/devece_type_model.dart';
import 'package:flutter_app_unionad/model/province_model.dart';
import 'package:flutter_app_unionad/model/work_take_model.dart';
import 'package:flutter_app_unionad/request/workbench_request.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/basice/select_sex_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_title_and_detail_widget.dart';
import 'package:flutter_app_unionad/ui/basice/show_select_address_dialog.dart';
import 'package:flutter_app_unionad/ui/workbench/widget/work_select_channel_widget.dart';
import 'package:flutter_app_unionad/ui/workbench/widget/work_select_device_number_widget.dart';
import 'package:flutter_app_unionad/ui/workbench/widget/work_select_model_widget.dart';

class WorkActivateWidget extends StatefulWidget {
  final String? id;
  final String? name;
  final String? phone;
  final WorkTakeModel? address;

  const WorkActivateWidget({
    Key? key,
    this.id,
    this.name,
    this.phone,
    this.address,
  }) : super(key: key);

  @override
  _WorkActivateWidgetState createState() => _WorkActivateWidgetState();
}

class _WorkActivateWidgetState extends State<WorkActivateWidget> {
  String? number;
  String? form;
  String? type;
  int userType = 1;
  String? more;
  String? name;
  String? iphone;
  String? address;
  ProvinceModel? provinceModel;
  ProvinceModel? cityModel;
  ProvinceModel? areaModel;
  ChannelModel? _channelModel;
  DeviceTypeModel? deviceTypeModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    iphone = widget.phone;
    name = widget.name;
    if (widget.address != null) {
      provinceModel = ProvinceModel(
        id: int.parse(widget.address!.userProvinceCode!),
        name: widget.address!.userProvince,
        shortName: widget.address!.userProvince,
      );
      cityModel = ProvinceModel(
        id: int.parse(widget.address!.userCityCode!),
        name: widget.address!.userCity,
        shortName: widget.address!.userCity,
      );
      areaModel = ProvinceModel(
        id: int.parse(widget.address!.userCountyCode!),
        name: widget.address!.userCounty,
        shortName: widget.address!.userCounty,
      );
      address = widget.address!.userAddress;
    }
  }

  void clear() {
    provinceModel = cityModel = areaModel = null;
    _channelModel = null;
    number = null;
    form = null;
    type = null;
    userType = 1;
    more = null;
    name = null;
    iphone = null;
    address = null;

    if (mounted) setState(() {});
  }

  void activateOnTap() async {
    if (number == null || number == '') {
      ShowToastWidget.showToast(msg: "请选择ICCID号");
      return;
    }
    if (deviceTypeModel == null || deviceTypeModel!.name == null || deviceTypeModel!.name!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请选择设备类型");
      return;
    }
    if (name == null || name!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请输入联系人");
      return;
    }

    if (iphone == null || iphone!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请输入联系人电话");
      return;
    }

    if (provinceModel == null) {
      ShowToastWidget.showToast(msg: "请选择区域");
      return;
    }

    if (address == null || address!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "请输入详细地址");
      return;
    }
    ProgressDialog.showProgress(context);
    final f = await WorkBenchRequest.initDev(
      channelId: _channelModel!.id,
      channelType: _channelModel!.visual,
      contractName: name,
      contractTel: iphone,
      createBy: null,
      devAddress: address,
      devCity: cityModel!.name,
      devCityCode: cityModel!.id!.toString(),
      devCounty: areaModel!.name,
      devCountyCode: areaModel!.id.toString(),
      devProvince: provinceModel!.name,
      devProvinceCode: provinceModel!.id.toString(),
      devSn: number,
      id: null,
      operType: _channelModel!.visual,
      userName: name,
      // userType: map[type],
      bookingId: widget.id,
      modelId: deviceTypeModel!.id.toString(),
    );
    print("结束");
    ProgressDialog.dismiss(context);
    if (f) {
      ShowToastWidget.showToast(msg: '绑定成功');
      if (widget.id != null) {
        NavigatorUtil.pop(context);
      } else {
        clear();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(
              horizontal: 20.px,
              vertical: 15.px,
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5),
            ),
            child: buildContent(),
          ),
        ),
        SizedBox(height: 20),
        MyButton(
          title: "激活",
          onTap: () {
            activateOnTap();
          },
        ),
        SizedBox(height: 20),
      ],
    );
  }

  Widget buildContent() {
    return SingleChildScrollView(
      child: Column(
        children: [
          SelectTitleAndDetailWidget(
            title: "ICCID号",
            detail: number != null ? number : "请选择ICCID号",
            icon: Icon(Icons.arrow_drop_down, size: 15.px),
            onTap: () => selectDevice(),
          ),
          MySizeBox(height: 10),
          SelectTitleAndDetailWidget(
            title: "渠道",
            detail: _channelModel?.channelName ?? "请选择渠道",
            icon: Icon(Icons.arrow_drop_down, size: 15.px),
            onTap: () => selectForm(),
          ),
          MySizeBox(height: 10),
          SelectTitleAndDetailWidget(
            title: "设备类型",
            detail: deviceTypeModel?.name ?? "请选择设备类型",
            icon: Icon(Icons.arrow_drop_down, size: 15.px),
            onTap: () => selectUserType(),
          ),
          MySizeBox(height: 10),
          SelectSexWidget(
            title: "用户类型",
            sex: userType,
            actions: ["一人一机", "一人多机"],
            selectSexOnTap: (sex) => sex = sex,
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            title: "联系人",
            placeholder: "请输入联系人",
            valueChange: (value) => name = value,
            controller: TextEditingController(text: name),
          ),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            title: "电话",
            placeholder: "请输入联系人电话",
            valueChange: (value) => iphone = value,
            keyboard: TextInputType.number,
            controller: TextEditingController(text: iphone),
          ),
          MySizeBox(height: 10),
          SelectTitleAndDetailWidget(
              title: "选择区域",
              detail: provinceModel != null ? provinceModel!.name! + cityModel!.name! + areaModel!.name! : "请选择区域",
              icon: Icon(Icons.arrow_drop_down, size: 15.px),
              onTap: () => selectCity()),
          MySizeBox(height: 10),
          InputSeparatorWidget(
            title: "详细地址",
            placeholder: "请输入详细地址",
            valueChange: (value) => address = value,
            controller: TextEditingController(text: address),
          ),
        ],
      ),
    );
  }

  void selectForm() {
    WorkSelectChannelWidget.show(context).then((value) {
      if (value != null && mounted)
        setState(() {
          _channelModel = value;
        });
    });
  }

  void selectType() {}

  void selectUserType() {
    //那用户类型 type 应该是这样的01-家庭按天 02-商用按天 03-商用按流量 04-合租按流量
    WorkSelectTypeModelWidget.show(context).then((value) {
      if (value != null && mounted)
        setState(() {
          deviceTypeModel = value;
        });
    });
  }

  void selectDevice() {
    WorkSelectDeviceNumberWidget.show(context).then((value) {
      if (value != null && mounted)
        setState(() {
          number = value;
        });
    });
  }

  void selectCity() {
    ShowSelectAddressDialog.showAddress(context, onSelected: (province, city, area) {
      provinceModel = province;
      cityModel = city;
      areaModel = area;
      setState(() {});
    });
  }
}
